package com.own.demo.geodemo;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.demo.geogrid.BoundingBoxUtil;
import com.own.demo.geogrid.UtilsGeoGrid;
import com.own.demo.utils.CommWriterFileInfo;
import com.own.demo.utils.DiagonalUtils;
import com.own.demo.utils.FileToStringUtils;
import com.own.demo.utils.RegionUtil;
import com.own.demo.utils.geo.PoiTypeEnums;
import com.own.demo.utils.geo.buffer.LineBufferImpl;
import com.own.demo.utils.geo.buffer.PolygonBufferImpl;
import com.own.demo.vo.Bounds;
import lombok.SneakyThrows;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.opengis.geometry.BoundingBox;

import java.io.BufferedWriter;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

public class ToGeo对角线2 {
    static int HEIGHT = 100;

    //每个格子的边长单位米
    static int sig=400;
    static LineBufferImpl defaultBuffer = (LineBufferImpl) PoiTypeEnums.POLYLINE.getBufferUtil();
    static PolygonBufferImpl polygonBuffer = new PolygonBufferImpl();

//    @Test
//    public void testGeoArea(){
//        System.out.println("开始执行");
//        File geoZJ = new File("E:\\ownCode\\emg-geojson\\src\\test\\java\\com\\own\\demo\\geodemo\\device.txt");
//        String geoData= FileToStringUtils.txt2String(geoZJ).split("\\|")[0];
//        String[] adasStr = geoData.split("\\,");
//        int row=adasStr.length;
//        System.out.println("行政区域围栏点数量："+row);
//
//        List<Double> lonList=new ArrayList(100000);
//        List<Double> latList=new ArrayList(100000);
//
//        for(int i=0;i<row;i++){
//            String[] points=adasStr[i].split(" ");
//            int pointLength=points.length;
//            if(pointLength>2){
//                System.out.println("错误采集点");
//                continue;
//            }
//            lonList.add(Double.valueOf(points[0]));
//            latList.add(Double.valueOf(points[1]));
//        }
//
//        System.out.println("lon长度："+lonList.size());
//        System.out.println("lat长度："+latList.size());
//
//        double minLon = Collections.min(lonList);
//        double maxLon = Collections.max(lonList);
//        double minLat = Collections.min(latList);
//        double maxLat = Collections.max(latList);
//        Bounds bounds=new Bounds(minLon,maxLon,minLat,maxLat);
//        try {
//            double distance=RegionUtil.getDistance(minLat,minLon,maxLat,maxLon);
//            BoundingBox boundingBox=  BoundingBoxUtil.getBoundingBox(minLon,maxLon,minLat,maxLat);
//            Geometry geometry= PoiTypeEnums.geometry(geoData, PoiTypeEnums.POLYGON.getCode());
//            System.out.println("对角线最长距离："+distance+" 米");
//            double height=RegionUtil.getDistance(minLat,maxLon,maxLat,maxLon);
//            System.out.println("高度距离："+height+" 米");
//            double distance1=RegionUtil.getDistance(minLat,maxLon,minLat,minLon);
//            System.out.println("边长距离："+distance1+" 米");
//
//           // double sidelenSize =  distance1/sig;
//
//            double sidelenSize=20;
//            System.out.println("对角线边数："+sidelenSize);
//            double d= UtilsGeoGrid.getSideLen(boundingBox,(int) sidelenSize);
//            System.out.println("角度："+d);
//
//
//            double sideLen = d;
//            List<Geometry> geometries=new UtilsGeoGrid().gridGeometryInfo(geometry,boundingBox,true,sideLen);
//            ToGeo对角线2.goGeoJSONV2(geometries,geometry,bounds,true);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * @param geometries 分成格子边界边界数据
     * @param geometry   全量边界
     * @param bounds
     * @param flag       false 只生成边界数据。 不生成osm文件  true 全量生成
     * @return
     */
    @SneakyThrows
    public static List<List<Coordinate>> goGeoJSONV2(List<Geometry> geometries, Geometry geometry) {
        if (geometries == null) {
            return null;
        }
        int row = geometries.size();
        List<Double> xList=new ArrayList<>();
        List<Double> yList=new ArrayList<>();

        //中心点对应的面
        Map<String,Polygon> pointPolygon=new HashMap<>(1103);
        for (int i = 0; i < row; i++) {
            Polygon js = (Polygon) geometries.get(i);
            Point point = js.getCentroid();
            String key=point.getX()+"_"+point.getY();
            pointPolygon.put(key,js);
            xList.add(point.getX());
            yList.add(point.getY());
        }
        TreeSet setXList = new TreeSet(xList);
        xList.clear();
        xList.addAll(setXList);
        TreeSet setYList = new TreeSet(yList);
        yList.clear();
        yList.addAll(setYList);
        xList = xList.stream().sorted(Comparator.comparing(Double::valueOf)).collect(Collectors.toList());
//        yList = yList.stream().sorted(Comparator.comparing(Double::valueOf)).collect(Collectors.toList());
        int rowX=xList.size();
        int rowY=yList.size();
        Coordinate[][] polygons=new Coordinate[rowX][rowY];

        for (int i = 0; i < rowX; i++) {
           double x=xList.get(i);
           for(int j=0;j<rowY;j++){
               polygons[i][j]=new Coordinate(x,yList.get(j));
           }
        }
        List<List<Coordinate>> allData = new ArrayList<>(1026);
        allData.addAll(DiagonalUtils.rightDiagonaLine(polygons,geometry,  pointPolygon));
        allData.addAll(DiagonalUtils.leftDiagonaLine(polygons,geometry,  pointPolygon));
        return allData;
    }


        public static String dataDraw(List<List<Coordinate>> list, Bounds bounds, List<List<List<Coordinate>>> listInfo, boolean flag) {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray(1024);
        jsonObject.put("type", "GeometryCollection");
        if (flag) {
            for (int i = 0; i < listInfo.size(); i++) {
                list.addAll(listInfo.get(i));
            }
        }
        int row = list.size();
        for (int i = 0; i < row; i++) {
            jsonArray.add(setGeoJSON(i, 1, list.get(i), "LineString"));
        }
        jsonObject.put("features", jsonArray);
        return jsonObject.toJSONString();
    }

        public static JSONObject setGeoJSON(int i, int j, List<Coordinate> list, String geoType) {
        JSONObject geo = new JSONObject(4);
        geo.put("type", "Feature");
        JSONObject geoPro = new JSONObject(1);
        geoPro.put("name", i > 0 ? j + "-" + i+"yyyyyyyyyyyy" : "hetest");
        geo.put("properties", geoPro);
        JSONObject objectInfo = new JSONObject();
        Coordinate[] coordinates = list.toArray(new Coordinate[0]);
        int coordRow = coordinates.length;
        List<double[]> gpsList = new ArrayList<>(coordRow);

        for (int h = 0; h < coordRow; h++) {
            Coordinate coordinate = coordinates[h];
            double[] gps = new double[2];
            gps[0] = coordinate.x;
            gps[1] = coordinate.y;
            gpsList.add(gps);
        }
        objectInfo.put("coordinates", gpsList);
        objectInfo.put("type", geoType);
        geo.put("geometry", objectInfo);
        return geo;
    }

        public static void writerFoFile(String data) {
        try {
            String folderPath = "E:\\ownCode\\graphhopper-web-7.0\\地图工具";
            File folder = new File(folderPath);
            String fileName = "\\polylineData1.js";
            if (!folder.exists()) {
                boolean result = folder.mkdirs();
            }
            BufferedWriter bufferedWriter = CommWriterFileInfo.getWriterBuffer(folderPath + "\\" + fileName, false);
            CommWriterFileInfo.writer(bufferedWriter, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String url01="E:\\ownCode\\UMA\\数据\\路网数据\\base\\测试数据01";
        String fileName="test01.osm";
        String head="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<osm version=\"0.6\" generator=\"CGImap 0.8.8 (587855 spike-08.openstreetmap.org)\" copyright=\"OpenStreetMap and contributors\" attribution=\"http://www.openstreetmap.org/copyright\" license=\"http://opendatacommons.org/licenses/odbl/1-0/\">";

        BufferedWriter bufferedWriter = CommWriterFileInfo.getWriterBuffer(url01 + "\\" + fileName, false);
        CommWriterFileInfo.writer(bufferedWriter, head);

        for(int i=0;i<100;i++){
            CommWriterFileInfo.writer(bufferedWriter, i+"");
        }


    }
}
